package com.jkgh.test {
		
	import com.jkgh.remedium.*;
	import com.jkgh.remedium.objects.*;
	import com.jkgh.remedium.objects.messages.*;
	import com.jkgh.remedium.objects.messages.ins.*;
	import com.jkgh.remedium.objects.messages.out.*;
	import com.jkgh.remedium.states.*;
	import com.jkgh.remedium.tools.*;
	
	/**
	 * The main server object.
	 * 
	 * @author RS2FGenerator
	 */
	public class MainServerObject extends RemediumDataObject {
	
		/**
		 * A name of this server.
		 */
		public var name:String;
		
		/**
		 * All versions supported by this server.
		 */
		public var versions:Vector.<int>;
		
		/**
		 * A parent server object. May be null to indicate top level server object.
		 */
		public var parent:MainServerObject;
	
		/**
		 * A server-side controller for this server object.
		 */
		public var controller:MainServerController;
		
		/**
		 * General purpose constructor.
		 * 
		 * @param producer the data producer to read from
		 * @param message the message object to pass on
		 */
		public function MainServerObject(name:String = null, versions:Vector.<int> = null, parent:MainServerObject = null, controller:MainServerController = null) {
			this.name = name;
			this.versions = versions;
			this.parent = parent;
			this.controller = controller;
		}
		
		// --------------------------------------------------------------------------------
		
		override public function produceContent(remedium:RemediumSystem, message:RemediumOutMessage):void {
			RemediumTools.produceString(remedium, name);
			RemediumTools.produceIntArray(remedium, versions);
			message.produceObject(parent, remedium);
		}
	
		public static function createConsumingState(message:RemediumInMessage, future:Function):RemediumState {
			return new GatheringState(1, function(remedium:RemediumSystem):void {
				
				switch (remedium.produceByte()) {

					case RemediumConstants.NULL:
						future(null);
						break;
						
					case RemediumConstants.HISTORY:
						future(MainServerObject(message.fromHistory(remedium.produceInt())));
						break;
						
					case RemediumConstants.VALUE:
						consumeContent(remedium, message, future);
						break;
				}
			});
		}
	
		private static function consumeContent(remedium:RemediumSystem, message:RemediumInMessage, future:Function):void {
			var ret:MainServerObject = new MainServerObject();
			message.toHistory(ret);
			RemediumTools.consumeString(remedium, function(rname:String):void {
	
				ret.name = rname;
				RemediumTools.consumeIntArray(remedium, function(rversions:Vector.<int>):void {

					ret.versions = rversions;
					remedium.setState(MainServerObject.createConsumingState(message, function(rparent:MainServerObject):void {

						ret.parent = rparent;
						remedium.setState(MainServerController.createConsumingState(message, function(rcontroller:MainServerController):void {
							
							ret.controller = rcontroller;
							future(ret);
						}));
					}));
				});
			});
		}
	}
}